package www.lagou.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import www.lagou.entity.Dept;
import www.lagou.entity.Employee;
import www.lagou.entity.Phone;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * 员工信息数据库操作
 *
 * @author Shuxian Huang
 */
public class EmployeeDao {

    DeptDao dd = new DeptDao();

    public List<Employee> findEmployees(String sql, Object...params) throws SQLException {

        //1、创建QueryRunner
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //2、执行查询
        List<Employee> employees = qr.query(sql, new BeanListHandler<Employee>(Employee.class), params);

        //3、查询对应的部门信息
        for (Employee employee: employees) {
            employee.setDept(dd.findDeptById(employee.getDid()));
        }
        return employees;
    }
}
